package com.huawei.spare.part.replace.framework;

import com.huawei.spare.part.replace.views.widgets.Models;
import com.jfoenix.controls.JFXDialog;
import io.reactivex.Observable;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import lombok.ToString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class SmartContentPresenter extends SmartPresenter {
    
    private static final Logger log = LoggerFactory.getLogger(SmartContentPresenter.class);


    protected SimpleObjectProperty<JFXDialog> loading = new SimpleObjectProperty<>();

    @FXML
    protected Pane container;


    /**
     * get ROOT stack-pane container for all content view
     *
     * @return
     */
    public StackPane root() {
        return SmartDataBus.get().getRootContentView();
    }


    /**
     * @param display
     * @param closable
     * @see Models#loading(StackPane, String, boolean)
     */
    public void loading(String display, boolean closable) {
        this.loading(display, closable, true);
    }

    /**
     * @param display
     * @param closable
     * @see Models#loading(StackPane, String, boolean)
     */
    public void loading(String display, boolean closable, boolean disableFooter) {
        final String message = bundle.containsKey(display) ? getString(display) : display;
        if (loading.get() == null) {
            final JFXDialog dialog = Models.loading(root(), message, closable);
            if (disableFooter) {
                dialog.setOnDialogOpened(e -> SmartRouter.get().disableFooterView(true));
                dialog.setOnDialogClosed(e -> SmartRouter.get().disableFooterView(false));
            }
            this.loading.set(dialog);
            dialog.show();
        } else {
            final VBox vbox = (VBox) loading.get().getContent();
            final Label messageLabel = (Label) vbox.getChildren().get(1);
            messageLabel.setText(message);
            if (!loading.get().isVisible()) {
                loading.get().show();
            }
        }
    }


    public void dismissLoading() {
        if (loading.get() != null) {
            loading.get().close();
        }
    }

    public void showDialog(JFXDialog dialog) {
        dialog.setOnDialogOpened(e -> SmartRouter.get().disableFooterView(true));
        dialog.setOnDialogClosed(e -> SmartRouter.get().disableFooterView(false));
        dialog.show();
    }

    /**
     * post handler before finish view (typically before switch to next step view)
     *
     * @return
     */
    public Observable<Boolean> onFinish() {
        return Observable.just(true);
    }

    /**
     * post handler before leave view (typically on previous step button clicked)
     *
     * @return
     */
    public Observable<Boolean> onLeave() {
        return Observable.just(true);
    }


}
